﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using Terralever.Facebook.UI.Design;
using System.Web.UI;

namespace Terralever.Facebook.UI.FbmlControls
{
    /// <summary>
    /// Represents an external inline frame, which can be used to render more complex styling.
    /// </summary>
    /// <remarks>
    /// <para>The Facebook documentation for the rendered control can be found
    /// <a href="http://wiki.developers.facebook.com/index.php/Fb:iframe" target="_blank">here</a>.</para>
    /// </remarks>
    public class Iframe : FbmlControl
    {
        /// <summary>
        /// Creates a new <see>Iframe</see>.
        /// </summary>
        public Iframe()
        {
            ShowBorder = true;
            ScrollMode = IframeScrollMode.Enabled;
        }

        /// <summary>
        /// Gets or sets the url displayed by this iframe.  This property is required.
        /// </summary>
        [Browsable(true)]
        [Category("Behavior")]
        [Description("Specifies the URL that should be loaded into this iframe.")]
        [DefaultValue(null)]
        [FbmlRequired(IsRequired = true)]
        public string Url
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets whether Facebook should attempt to size the frame by its content size.
        /// </summary>
        [Browsable(true)]
        [Category("Behavior")]
        [Description("Specifies whether the iframe should be sized according to its content by Facebook.")]
        [DefaultValue(false)]
        public bool SmartSize
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets whether to display the frame border.
        /// </summary>
        [Browsable(true)]
        [Category("Behavior")]
        [Description("Specifies whether to show the frame border.")]
        [DefaultValue(true)]
        public bool ShowBorder
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the scrolling mode for the iframe.
        /// </summary>
        [Browsable(true)]
        [Category("Behavior")]
        [Description("Specifies the scrolling type used for the iframe.")]
        [DefaultValue(IframeScrollMode.Enabled)]
        public IframeScrollMode ScrollMode
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets whether the iframe should be resizable.
        /// </summary>
        [Browsable(true)]
        [Category("Behavior")]
        [Description("Specifies whether the iframe should be resizable.")]
        [DefaultValue(false)]
        public bool Resizable
        {
            get;
            set;
        }

        /// <inheritdoc />
        protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);

            if (string.IsNullOrEmpty(Url))
                throw new RequiredFbmlAttributeNotSetException("Url", Url);
            else
                writer.AddAttribute(HtmlTextWriterAttribute.Src, Url);

            if (SmartSize)
                writer.AddAttribute("smartsize", "true");

            if (!ShowBorder)
                writer.AddAttribute("frameborder", "0");

            switch (ScrollMode)
            {
                case IframeScrollMode.Auto:
                    writer.AddAttribute("scrolling", "auto");
                    break;
                case IframeScrollMode.Disabled:
                    writer.AddAttribute("scrolling", "no");
                    break;
                case IframeScrollMode.Enabled:
                default:
                    // do nothing - 'yes' is default.
                    break;
            }
        }

        /// <inheritdoc />
        protected internal override string ElementName
        {
            get { return FbmlConstants.FB_IFRAME; }
        }
    }
}
